package com.atm.atmsys.controller.user;


import com.atm.atmsys.Service.AccountService;
import com.atm.atmsys.Service.UserService;
import com.atm.atmsys.dto.UserDTO;
import com.atm.atmsys.dto.UserPageDTO;
import com.atm.atmsys.entity.AdminInfo;
import com.atm.atmsys.entity.PageResult;
import com.atm.atmsys.entity.UserInfo;
import com.atm.atmsys.utils.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private AccountService accountService;

    @PostMapping("/addUser")
    @ApiOperation("新增用户")
    public Result addUser(@RequestBody UserInfo userInfo) {
        log.info("新增银行用户:{}",userInfo);
        userService.addUser(userInfo);
        return Result.success();
    }

    @DeleteMapping("/deleteUserAndAccounts/{identification}")
    @ApiOperation("删除用户")
    @Transactional(rollbackFor = Exception.class)
    public Result deleteAdmin(@PathVariable String identification) {
    try {
            if (userService.getByIdentification(identification) != null) {
                //删除用户所有的账户
                List<String> cardNos = accountService.getCardNOByIdentification(identification);
                for (String cardNo : cardNos) {
                    if (accountService.getAccountStatusByCardNO(cardNo).equals("active")) {
                        accountService.deleteAccount(cardNo);
                    } else {
                        throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "账户" + cardNo + "已被冻结，无法删除，请联系管理员");
                    }
                }
                userService.deleteAdmin(identification);
                return Result.success();
            } else {
                return Result.error("用户不存在");
            }
        }catch (ResponseStatusException e) {
        log.error("删除用户时发生错误: ", e);
        return Result.error("该用户名下有冻结账户");
    }
    }

    @PutMapping("/updateUser")
    @ApiOperation("根据Id更新用户")
    public Result updateUser(@RequestBody UserDTO userDTO) {
        log.info("更新用户信息:{}",userDTO);
        userService.updateUser(userDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询用户")
    public Result<PageResult> page(UserPageDTO userPageDTO){
        log.info("用户分页查询，参数为：{}",userPageDTO);
        PageResult pageResult=userService.pageQuery(userPageDTO);
        return Result.success(pageResult);

    }

    @GetMapping("/{id}")
    @ApiOperation("根据Id查询用户")
    public Result<UserInfo> getById(@PathVariable Integer id){
        log.info("根据Id查询用户，id为：{}",id);
        UserInfo userInfo=userService.getById(id);
        return Result.success(userInfo);
    }

    @GetMapping("/selectByIdentification/{identification}")
    @ApiOperation("根据身份证查询用户")
    public Result<UserInfo> getByIdentification(@PathVariable String identification){
        log.info("根据身份证查询用户，身份证为：{}",identification);
        UserInfo userInfo=userService.getByIdentification(identification);
        return Result.success(userInfo);
    }

    @GetMapping("identification/{identification}")
    @ApiOperation("根据身份证号码查询用户")
    public Result<UserInfo> getBySF(@PathVariable String identification) {
        log.info("根据身份证号码查询用户，id为：{}", identification);
        UserInfo userInfo = userService.getBySF(identification);
        return Result.success(userInfo);
    }




}
